package com.game.view.display
{
    import com.game.view.IAnimation;
    
    import flash.display.Bitmap;
    import flash.display.BitmapData;
    import flash.geom.Point;
    
    /**
     * 只有一帧的Bitmap动画
     */
    public class BitmapClip extends AnimationClip implements IAnimation
    {
        
        private var _ts:Texture;
        private var _bp:Bitmap;
        
        public function BitmapClip(tex:* = null)
        {
            super();
            setTexture(tex);
        }
        
        public function setTexture(tex:*):void
        {
            if(!tex) return;
            if(tex is TextureGroup) _ts = (tex as TextureGroup).getTextureAt(0);
            else if (tex is Texture) _ts = tex;
            else if (tex is BitmapData) _ts = new Texture(tex);
            init();
        }
        
        private function init():void
        {
            _totalFrames = 1;
            _bp = new Bitmap(null, "auto", true);
            addChild(_bp);
        }
        
        override protected function render():void
        {
            super.render();
            _currentFrame = 1;
            _bp.bitmapData = _ts.content;
            updatePosition();
        }
        
        public function updatePosition():void
        {
            if (_bp.x == _ts.pivotX && _bp.y == _ts.pivotY)
            {
                return;
            }
            _bp.x = _ts.pivotX;
            _bp.y = _ts.pivotY;
        }
        
        override public function get totalFrame():int
        {
            return 1;
        }
        
        override public function  Destroy():void
        {
            removeFromParent();
            _ts.dispose();
            _ts = null;
        }
    }
}